<!--#
layout("/layouts/platform.html"){
#-->
<header class="header navbar bg-white shadow">
	<div class="btn-group tool-button">
		<a class="btn btn-primary navbar-btn" href="${base}/platform/sys/menu"
			data-pjax><i class="ti-angle-left"></i>
			${msg['globals.button.back']}</a>
	</div>
</header>

<div class="content-wrap">
	<div class="wrapper" style="min-height: 500px;">
		<section class="panel panel-form">
			<form id="unitAddForm" role="form"
				class="form-horizontal parsley-form" data-parsley-validate
				action="${base}/platform/sys/menu/addDo" method="post">
				<div class="row mb10 mt10">
					<div class="col-lg-12">
						<div class="form-group">
							<label for="name" class="col-sm-2 control-label">${msg['sys.menu.column.system']}</label>

							<div class="col-sm-8">
								<input type="text" id="showSystem" class="form-control"
									data-parsley-required="true" disabled
								<!--#if(system =="platform"){#-->
								value="${msg['sys.menu.column.platform']}"
								<!--#}else{#-->
								value="${msg['sys.menu.column.merchants']}"
								<!--#}#-->
								>
							</div>
						</div>

						<div class="form-group has-feedback">
							<label for="parentId" class="col-sm-2 control-label">${msg['sys.menu.column.highermenu']}</label>

							<div class="col-sm-8">
								<div class="input-group">
									<input id="parentId" type="text" class="form-control"
										placeholder="${msg['sys.menu.column.notchoosedefault']}"
										disabled
										value="<!--#if(!isEmpty(obj)){#-->${obj.name}<!--#}#-->" /> <span
										class="input-group-btn">
										<button type="button" class="btn btn-primary"
											data-toggle="modal" data-target="#dialogSelectParentUnit">
											<i class="ti-plus"></i>${msg['sys.menu.column.select']}
										</button>
									</span>
								</div>
								<input type="hidden" name="system"
									value="<!--#if(!isEmpty(system)){#-->${system}<!--#}#-->">
								<input type="hidden" name="type"
									   value="menu">
								<input type="hidden" name="parentId"
									value="<!--#if(!isEmpty(obj)){#-->${obj.id}<!--#}#-->">
								<input type="hidden" id="setRoleVal" name="setRoleVal" value="1">
							</div>
						</div>
						<div class="form-group">
							<label for="name" class="col-sm-2 control-label">${msg['sys.menu.column.name']}</label>

							<div class="col-sm-8">
								<input type="text" id="name" class="form-control" name="name"
									   data-parsley-required="true"
									   placeholder="${msg['sys.menu.column.name']}">
							</div>
						</div>
						<div class="form-group">
							<label for="name" class="col-sm-2 control-label">${msg['sys.menu.column.aliasName']}</label>

							<div class="col-sm-8">
								<input type="text" id="aliasName" class="form-control" name="aliasName"
									   data-parsley-required="true"
									   placeholder="${msg['sys.menu.column.aliasName']}">
							</div>
						</div>
						<div class="form-group">
							<label for="permission" class="col-sm-2 control-label">${msg['sys.menu.column.permissionidentify']}</label>

							<div class="col-sm-8">
								<input type="text" id="permission" name="permission"
									class="form-control"
									placeholder="${msg['sys.menu.column.permissionidentify']}"
									data-parsley-required="true">

								<p class="help-block no-m">${msg['sys.menu.column.forexample']}</p>
							</div>
						</div>
						<div id="divId">
							<div class="form-group">
								<label for="href" class="col-sm-2 control-label">${msg['sys.menu.column.url']}</label>

								<div class="col-sm-8">
									<input type="text" id="href" name="href" class="form-control"
										placeholder="URL">
								</div>
							</div>
							<div class="form-group">
								<label for="target" class="col-sm-2 control-label">target</label>
								<div class="col-sm-8">
									<input type="text" id="target" name="target"
										class="form-control" placeholder="data-pjax">
								</div>
							</div>
							<div class="form-group">
								<label for="icon" class="col-sm-2 control-label">${msg['sys.menu.column.icon']}</label>
								<div class="col-sm-8">
									<input id="icon" type="text" name="icon" class="form-control"
										placeholder="${msg['sys.menu.column.iconstyle']}">
									<p class="help-block no-m">
										${msg['sys.menu.column.resultspreview']}<i id="iconPreview"
											class="ti-plus"></i>
									</p>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label for="disabled" class="col-sm-2 control-label">${msg['sys.menu.column.isdisable']}</label>
							<div class="col-sm-8 switcha">
								<div class="mr15">
									<input type="checkbox" id="disabled" name="disabled"
										class="js-switch-blue">
								</div>
							</div>
						</div>

						<div class="form-group">
							<label for="setRole" class="col-sm-2 control-label">${msg['sys.menu.column.setdataaccess']}</label>
							<div class="col-sm-8 switcha">
								<div class="mr15">
									<input type="checkbox" id="setRole" class="js-switch-blue">
								</div>
							</div>
						</div>

						<div class="form-group" id="dataRoleDiv">
							<label class="col-sm-2 control-label control-label">${msg['globals.button.dataaccess']}</label>
							<div class="row">
								<div class="col-sm-4">

									<div class="checkbox"></div>
								</div>

								<div class="col-sm-3">
									<div class="control-group">
										<div class="controls">
											<label class="control-label"></label> <input id="dataName"
												type="text" class="form-control input-sm mb20"
												placeholder="${msg['sys.menu.column.accessname']}"> <input
												id="dataPermission" type="text"
												class="form-control input-sm mb20"
												placeholder="${msg['sys.menu.column.accesslogo']}">
											<button type="button" class="btn btn-primary" id="addData">
												<i class="ti-plus"></i>${msg['sys.menu.column.add']}
											</button>
										</div>
									</div>
								</div>
							</div>
						</div>


					</div>
				</div>
				<div class="col-lg-3"></div>
				<div class="col-lg-6">
					<div class="form-group text-center">
						<label></label>

						<div>
							<button class="btn btn-primary btn-block btn-lg btn-parsley"
								data-loading-text="${msg['sys.menu.column.nowsubmit']}">${msg['sys.menu.column.submit']}
							</button>
						</div>
					</div>
				</div>
			</form>
		</section>

	</div>
</div>
<a class="exit-offscreen"></a>
<!-- 选择上级单位 -->
<div id="dialogSelectParentUnit" class="modal fade bs-modal-sm"
	tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">×</button>
				<h4 class="modal-title">${msg['sys.menu.column.selectupmenu']}</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						<div id="jsTreeParentUnit" class="demo"></div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<div class="pull-left">
					<button type="button" class="btn btn-success" data-dismiss="modal"
						onclick="selectFirstMenu()">
						${msg['sys.menu.column.setupmenu']}</button>
				</div>
				<button type="button" class="btn btn-default" data-dismiss="modal">${msg['sys.menu.column.cancel']}</button>
				<button type="button" class="btn btn-primary"
					onclick="selectParentMenu()">${msg['sys.menu.column.soureselect']}</button>
			</div>
		</div>
	</div>
</div>
<script language="JavaScript">

    function initTreeView() {
        $("#jsTreeParentUnit").jstree({
            plugins: ["wholerow", "json_data"],
            core: {
                data: {
                    dataType: "json",
                    url: function (node) {
                        return node.id === "#" ? "${base}/platform/sys/menu/tree?system=${system!}" : "${base}/platform/sys/menu/tree/" + node.id + "?system=${system!}"
                    }
                },
                multiple: false
            }
        }).on("dblclick.jstree", function (node) {
            selectParentMenu();
        });
    }
    //选择父菜单
    function selectParentMenu() {
        var tree = $.jstree.reference("#jsTreeParentUnit");
        var node = tree.get_selected(true);
        $("#unitAddForm #parentId").val(node[0].text);
        $("#unitAddForm input[name='parentId']").val(node[0].id);
        $("#dialogSelectParentUnit").modal("hide");
    }
    function selectFirstMenu() {
        $("#unitAddForm #parentId").val("顶级菜单");
        $("#unitAddForm input[name='parentId']").val("");
        $("#dialogSelectParentUnit").modal("hide");
    }
    $(document).ready(function () {
        initTreeView();
        myForm.init();
        $("#type").on("change", function () {
            if ($(this).val() == "menu") {
                $("#divId").show();
            } else {
                $("#href").val("");
                $("#target").val("");
                $("#icon").val("");
                $("#divId").hide();
            }
        });
        //图标样式 实时预览
        $("#icon").on("keyup", function () {
            $("#iconPreview").attr("class", $(this).val());
        });
        $('#unitAddForm').ajaxForm({
            dataType: 'json',
            beforeSubmit: function (arr, form, options) {
                if ($("#type").val() == "data" && $("#parentId").val() == "") {
                    Toast.error("数据类型必须选择上级菜单");
                    return false;
                }
                form.find("button:submit").button("loading");
            },
            success: function (data, statusText, xhr, form) {
                if (data.code == 0) {
                    Toast.success(data.msg);
                    //刷新父级菜单
                    $.jstree.reference("#jsTreeParentUnit").refresh();
                    var parentName = $("#unitAddForm #parentId").val();
                    form.resetForm();
                    $("#unitAddForm #parentId").val(parentName);
                } else {
                    Toast.error(data.msg);
                }
                form.find("button:submit").button("reset");
            }
        });
    });

    $("#addData").on("click", function () {
        var dataName = $("#dataName").val();
        var dataPermission = $("#dataPermission").val();
        if (dataName && dataPermission) {
            $(".checkbox").append("<div class='mt25 mb5'>" +
                    "<input type='checkbox' onclick='checkboxOnclick($(this))' class='checkbox' checked='checked'>" +
                    "<input type='hidden'  name='listChildMenu.name' value='" + dataName + "'>" +
                    "<input type='hidden'  name='listChildMenu.permission' value='" + dataPermission + "'>" +
                    "<input type='hidden'  name='listChildMenu.disabled' value='0'>" +
                    "<label   class='ml5'>" + dataName + "&nbsp;["+dataPermission+"]&nbsp;</label>" +
                    "<a href='javascript:;' class='btn btn-danger btn-xs btn-ge' onclick='delChild($(this))'>×</a>" +
                    "</div>"
            );

            $("#dataName").val("");
            $("#dataPermission").val("");

        }


    });


    $("#setRole").change(function () {
		var tempP = $("#permission").val();
		if(!tempP){
			$("#setRole").prop("checked",false);
			console.log($("#setRole").parent().html());
			$("#unitAddForm").attr("action", "${base}/platform/sys/menu/addDo/1");
			$("#permission").focus();
			Toast.error("权限标识不能为空");
		}

        if ($("#setRole").prop('checked')) {
            if(tempP){
                $(".checkbox").append(

                        "<div class='mt25 mb5'>" +
                        "<input type='checkbox' onclick='checkboxOnclick($(this))' class='checkbox'" +
                        "checked='checked'>" +
                        "<input type='hidden' name='listChildMenu.name' value='添加'>" +
                        "<input type='hidden' name='listChildMenu.permission' value='"+tempP+".add'>" +
                        "<input type='hidden' name='listChildMenu.disabled' value='0'>" +
                        "<label class='ml5'>添加 ["+tempP+".add]&nbsp; </label>" +
                        "<a href='javascript:;' class='btn btn-danger btn-xs btn-ge '" +
                        "onclick='delChild($(this))'>×</a>" +
                        "</div>" +

                        "<div class='mt25 mb5'>" +
                        "<input type='checkbox' onclick='checkboxOnclick($(this))' class='checkbox'" +
                        "checked='checked'>" +
                        "<input type='hidden' name='listChildMenu.name' value='修改'>" +
                        "<input type='hidden' name='listChildMenu.permission' value='"+tempP+".edit'>" +
                        "<input type='hidden' name='listChildMenu.disabled' value='0'>" +
                        "<label class='ml5'>修改 ["+tempP+".edit]&nbsp; </label>" +
                        "<a href='javascript:;' class='btn btn-danger btn-xs btn-ge '" +
                        "onclick='delChild($(this))'>×</a>" +
                        "</div>" +

                        "<div class='mt25 mb5'>" +
                        "<input type='checkbox' onclick='checkboxOnclick($(this))' class='checkbox'" +
                        "checked='checked'>" +
                        "<input type='hidden' name='listChildMenu.name' value='删除'>" +
                        "<input type='hidden' name='listChildMenu.permission' value='"+tempP+".delete'>" +
                        "<input type='hidden' name='listChildMenu.disabled' value='0'>" +
                        "<label class='ml5'>删除 ["+tempP+".del]&nbsp; </label>" +
                        "<a href='javascript:;' class='btn btn-danger btn-xs btn-ge '" +
                        "onclick='delChild($(this))'>×</a>" +
                        "</div>"
                )

                $("#dataRoleDiv").show();
                $("#setRoleVal").val("0");
                $("#unitAddForm").attr("action", "${base}/platform/sys/menu/addDo/0");
            }else {
				$("#setRole").prop("checked",false);
				console.log($("#setRole").parent().html());
				$("#unitAddForm").attr("action", "${base}/platform/sys/menu/addDo/1");
                $("#permission").focus();
                Toast.error("权限标识不能为空");
            }


        } else {
            $(".checkbox").html("");
            $("#dataRoleDiv").hide();
            $("#setRoleVal").val("1");
            $("#unitAddForm").attr("action", "${base}/platform/sys/menu/addDo/1");
        }

    });

    function delChild(child) {
        child.parent().remove();
    }

    function checkboxOnclick(checkbox) {
        if (checkbox.prop("checked")) {
            checkbox.next().next().next().val("0");
        } else {
            checkbox.next().next().next().val("1");
        }
    }

    //    $('body').on('click', '.btn-ge', function () {
    //        $(this).parent().remove();
    //    });

    $(function () {
        $("#dataRoleDiv").hide();

    });
</script>

<!--#}#-->


